package com.nari.ysphd.util.log;

import android.content.Context;
import android.net.TrafficStats;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.jeremyfeinstein.event.HDMsgEvent;
import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.NetUtils.AppGlobals;
import com.jeremyfeinstein.utils.NetUtils.SGApiService;
import com.jeremyfeinstein.utils.SPUtil;

import de.greenrobot.event.EventBus;

/**
 * @author : lishan
 * @e-mail : cnlishan@163.com
 * @date : 2021/3/4 10:52 AM
 * @desc :
 */
public class HDMsgUtils {
    private static final String TAG = HDMsgUtils.class.getSimpleName();
    public static String baseUrl;
    public static String token;

    private HDMsgUtils() {

    }

    private static class SingletonHolder {
        private static final HDMsgUtils INSTANCE = new HDMsgUtils();
    }

    public static final HDMsgUtils getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void init() {
        String Platform_IP = (String) SPUtil.get(AppConstants.Platform_IP, "");
        String Platform_PORT = (String) SPUtil.get(AppConstants.Platform_PORT, "");
        baseUrl = "http://" + Platform_IP + ":" + Platform_PORT + "/uvp-backend-consultation/api/equipment/";
        SGApiService.init(baseUrl, null);
    }

    public static long getTotalBytes() {
        long uidRxBytes = TrafficStats.getUidRxBytes(AppGlobals.getApplication().getApplicationInfo().uid);
        long uidTxBytes = TrafficStats.getUidTxBytes(AppGlobals.getApplication().getApplicationInfo().uid);
        long startBytes = uidRxBytes + uidTxBytes;
        Log.e(TAG, " uidRxBytes = " + uidRxBytes);
        Log.e(TAG, " uidTxBytes = " + uidTxBytes);
        Log.e(TAG, " startBytes = " + startBytes);
        return startBytes;
    }

    public static String getDevCode() {
        TelephonyManager tm = (TelephonyManager) AppGlobals.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
        String devCode = tm.getDeviceId();

        if (TextUtils.isEmpty(devCode)) {
            devCode = Settings.Secure.getString(AppGlobals.getApplication().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return devCode;
    }

    public static String getDevName() {
        String devName;
        try {
            devName = Build.class.getField("SERIAL").get(null).toString();
        } catch (Exception e) {
            devName = e.getMessage().hashCode() + "";
        }

        String brand = Build.BRAND;
        return brand + devName;
    }

    public static void stop() {
        HDMsgEvent event = new HDMsgEvent();
        event.MsgType = HDMsgEvent.Type.stop;
        EventBus.getDefault().post(event);
    }

    public static void start() {
        HDMsgEvent event = new HDMsgEvent();
        event.MsgType = HDMsgEvent.Type.start;
        EventBus.getDefault().post(event);
    }
}
